Imports System.Windows.Forms
Imports Microsoft.Win32
Imports System.Drawing.Printing

Public Class frmMain

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim defaultPrinter As String
        Call Main()
        defaultPrinter = listaStampanti()
        staBar.Items(2).Text = "Stampa su: " & defaultPrinter
        staBar.Items(3).Text = Now.Date
    End Sub

    Private Sub mnuClienti_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClienti.Click, tButton2.Click
        frmCliente.Show()
    End Sub

    Private Sub mnuContratti_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuContratti.Click, tButton1.Click
        frmContratti.Show()
    End Sub

    Private Sub mnuRicerche_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuRicerche.Click, tButton4.Click
        frmCalcoloTotali.Show()
    End Sub

    Private Sub mnuContrattiNonValidi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuContrattiNonValidi.Click, tButton5.Click
        frmContrattiInvalidati.ShowDialog()
    End Sub

    'Private Sub mnuApriGlv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuApriGlv.Click, tButton6.Click
    ' Dim percorso As String = Registry.CurrentUser.OpenSubKey("Software", False).OpenSubKey("Glv").GetValue("pathGlv").ToString()
    '    Process.Start(percorso & "\Gestione lampade votive .NET.exe")
    'End Sub

    Private Function listaStampanti() As String
        ' mostra la lista delle stampanti installate sul pc
        ' e restituisce quella di default
        Dim printers As New Printing.PrintDocument()
        Dim printername = printers.PrinterSettings.PrinterName
        Dim defaultPrinter As String = printername
        For Each printername In PrinterSettings.InstalledPrinters
            Dim TSMI As New ToolStripMenuItem
            TSMI.Text = printername
            If printername = defaultPrinter Then
                TSMI.Checked = True
            End If
            mnuStampanti.DropDownItems.Add(TSMI)
            AddHandler TSMI.Click, AddressOf TSMI_Click
        Next
        Return defaultPrinter
    End Function

    Private Sub TSMI_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim mnu As ToolStripMenuItem
        Dim mnuAppo As ToolStripMenuItem
        mnu = DirectCast(sender, ToolStripMenuItem)

        mnu.Checked = Not (mnu.Checked)
        staBar.Items(2).Text = "Stampa su: " & mnu.Text
        For Each mnuAppo In mnuStampanti.DropDownItems
            If mnuAppo.Text <> mnu.Text Then
                mnuAppo.Checked = False
            End If
        Next

    End Sub

    Private Sub mnuDati_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuDati.Click
        frmTabelle.Show()
    End Sub

    Private Sub mnuSpaziature_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSpaziature.Click
        frmSpaziatureStampaContratti.Show()
    End Sub
End Class
